package cn.zhentao.config;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

public class MyBatisConfig {

    //Spring整合mybatis
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        //设置别名
        ssfb.setTypeAliasesPackage("cn.zhentao.pojo");
        //设置数据源
        ssfb.setDataSource(dataSource);
        //设置mapper映射文件   待编写
        ssfb.setMapperLocations(new ClassPathResource("mapper/TeacherMapper.xml"));

        //设置驼峰命名
        Configuration configuration = new Configuration();
        configuration.setMapUnderscoreToCamelCase(true);

        ssfb.setConfiguration(configuration);

        return ssfb;
    }

    //配置dao的实现类
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("cn.zhentao.dao");
        return msc;
    }

}
